package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.KeyValueVo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.SysUserMenu;
import com.ruoyi.system.service.ISysUserMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户菜单关系Controller
 * 
 * @author WJH
 * @date 2022-08-02
 */
@RestController
@RequestMapping("/system/SysUserMenu")
public class SysUserMenuController extends com.ruoyi.common.core.web.controller.BaseController
{
    @Autowired
    private ISysUserMenuService sysUserMenuService;

    /**
     * 查询用户菜单关系列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserMenu:list")
    //@ApiOperation(value = "获取用户菜单关系列表")
    @GetMapping("/getSysUserMenuList")
    public com.ruoyi.common.core.web.page.TableDataInfo getSysUserMenuList(SysUserMenu sysUserMenu)
    {
        startPage();
        List<SysUserMenu> list = sysUserMenuService.selectSysUserMenuList(sysUserMenu);
        return getDataTable(list);
    }


    /**
     * 获取用户菜单关系详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserMenu:query")
    //@ApiOperation(value = "获取用户菜单关系详情")
    @GetMapping(value = "/getSysUserMenuById")
    public com.ruoyi.common.core.web.domain.AjaxResult getSysUserMenuById(@RequestParam("userId") Long userId)
    {
        return com.ruoyi.common.core.web.domain.AjaxResult.success(sysUserMenuService.selectSysUserMenuByUserId(userId));
    }

    /**
     * 新增用户菜单关系
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserMenu:add")
    @Log(title = "用户菜单关系", businessType = BusinessType.INSERT)
    //@ApiOperation(value = "新增用户菜单关系")
    @PostMapping(value = "/addSysUserMenu")
    public com.ruoyi.common.core.web.domain.AjaxResult addSysUserMenu(@RequestBody SysUserMenu sysUserMenu)
    {
        return toAjax(sysUserMenuService.insertSysUserMenu(sysUserMenu));
    }

    /**
     * 修改用户菜单关系
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserMenu:edit")
    @Log(title = "用户菜单关系", businessType = BusinessType.UPDATE)
    //@ApiOperation(value = "修改用户菜单关系")
    @PostMapping(value = "/editSysUserMenu")
    public com.ruoyi.common.core.web.domain.AjaxResult editSysUserMenu(@RequestBody SysUserMenu sysUserMenu)
    {
        return toAjax(sysUserMenuService.updateSysUserMenu(sysUserMenu));
    }

    /**
     * 删除用户菜单关系
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserMenu:remove")
    @Log(title = "用户菜单关系", businessType = BusinessType.DELETE)
	@GetMapping("/removeSysUserMenus")
    public com.ruoyi.common.core.web.domain.AjaxResult removeSysUserMenus(@RequestParam Long[] userIds)
    {
        return toAjax(sysUserMenuService.deleteSysUserMenuByUserIds(userIds));
    }
    @GetMapping("/getSysUserMenuNamesForSelect")
    //@ApiOperation(value = "获取用户菜单关系名称列表用于下拉搜索")
    public com.ruoyi.common.core.web.domain.AjaxResult getSysUserMenuNamesForSelect()
    {
        List<SysUserMenu> list = sysUserMenuService.list();//自行加条件
        List<KeyValueVo> resultList = new ArrayList<>();
        for (SysUserMenu sysUserMenu: list ) {
            KeyValueVo vo = new KeyValueVo();
            //vo.setKey(it.getId());
            //vo.setValue(it.getName());
            resultList.add(vo);
        }
        return com.ruoyi.common.core.web.domain.AjaxResult.success(resultList);
    }
    /**
     * 导出用户菜单关系列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "system:SysUserMenu:export")
    @Log(title = "用户菜单关系", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/exportSysUserMenu", produces = "application/octet-stream")
    public String exportSysUserMenu(HttpServletResponse response, SysUserMenu sysUserMenu) throws IOException
    {
        List<SysUserMenu> list = sysUserMenuService.selectSysUserMenuList(sysUserMenu);
        com.ruoyi.common.core.utils.poi.ExcelUtil<SysUserMenu> util = new com.ruoyi.common.core.utils.poi.ExcelUtil<SysUserMenu>(SysUserMenu.class);
        util.exportExcel(response, list, "用户菜单关系数据");
        return "";
    }

}
